define(['jquery', 'bootstrap', 'frontend', 'form', 'template'], function ($, undefined, Frontend, Form, Template) {
    var validatoroptions = {
        invalid: function (form, errors) {
            $.each(errors, function (i, j) {
                Layer.msg(j);
            });
        }
    };
    var Controller = {
        index: function () {
             // 更换虚拟形象
            $('.avatar-thumbnail').click(function(){
                var thumbnailSrc = $(this).attr('src');
                var src=thumbnailSrc.replace('thumbnail','transparent');
                $("#avatar").attr("src", src);
                // 去除路径首位获得人名和姿态
                var person=thumbnailSrc.replace('-thumbnail.png','');
                person=person.replace('/assets/addons/human/img/','')
                var arr=person.split('-');
                console.log(arr)
                var char=arr[0];
                char=char.charAt(0).toUpperCase() + char.slice(1);
                var style=arr[1]
                if(arr.length>2){
                    style=style+'-'+arr[2]
                }
                $("input[name='character']").val(char);
                $("input[name='style']").val(style);
            })
            // 更换背景
            $('.bg-thumbnail').click(function(){
                var color=$(this).data('bg');
                $("#avatar").parent().css("background-color", color);
                $("input[name='bg']").val(color+'FF');
            })
            //本地验证未通过时提示
            $("#synthesis-form").data("validator-options", validatoroptions);
            Form.api.bindevent($("#synthesis-form"),function(data,ret){
                
                Layer.confirm('生成视频大概需要3-5分钟，您可以点击【确定】跳转到任务列表查看生成结果',{},function(){
                    location.href="/index/human/job"
                })
                return false;
            },function(data,ret){
                Layer.confirm(ret.msg,{},function(index){
                    if(ret.url){
                        location.href=retlurl
                    }else{
                        Layer.close(index)
                    }
                    
                })
                return false;
            });
            
        },
        job: function () {
            require(['table'], function (Table) {

                // 初始化表格参数配置
                Table.api.init({
                    extend: {
                        index_url: 'human/job',
                    }
                });
                var urlArr = [];
                var multiple = Fast.api.query('multiple');
                multiple = multiple == 'true' ? true : false;

                var table = $("#table");


                // 初始化表格
                table.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    sortName: 'id',
                    showToggle: false,
                    showExport: false,
                    fixedColumns: true,
                    fixedRightNumber: 1,
                    columns: [
                        [
                            {field: 'id', title: __('Id'), operate: false},
                            {field: 'job_id', title: '任务ID', operate: false},
                            // {field: 'status', title: '状态', operate: false,formatter: function(value,row,index){
                            //     var status={pending:'处理中',success:'处理结束'};
                            //     return status[value]
                            // }},
                            {field: 'output', title: '视频链接', formatter: Controller.api.formatter.video},
                            {field: 'createtime', title: '创建时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                            // {field: 'updatetime', title: '完成时间',  formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        ]
                    ]
                });

               
                // 为表格绑定事件
                Table.api.bindevent(table);
                // setInterval(function(){
                //     $('.btn-refresh').trigger('click')
                // },30*1000)

            });
        },
        api:{
            formatter:{
                video:function(value,row,index){
                // return '<video controls style="width:400px" src="'+value+'" />'
                if(!value){
                    return '处理中'
                }
                return '<a  href="'+value+'" >下载链接</a>'
            }
            }
        }

          
       
       
        
    };
    return Controller;
});
